package sisi.invoices;

import java.util.List;

import javax.persistence.EntityManager;
/**
 * InvoicecounterController.java - Controller class for the <i>invoicecounter's database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 07/12/2014
 * 
 */
public class InvoicecounterController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Invoicecounter[] get(String cerca) {
		List<Invoicecounter> lista = getList(cerca);
			return lista.toArray(new Invoicecounter[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Invoicecounter> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.invnumb) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Invoicecounter as c " + cWhere 
					+ " order by c.id ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Invoicecounter> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	
	public Integer lastId() {

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select MAX(c.id) from Invoicecounter as c");
			Object risultato = q.getSingleResult();
			return (Integer)risultato;
		} finally {
			em.close();
		}
	}
	
	public Invoicecounter getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Invoicecounter invoicecounterx = em.find(Invoicecounter.class, id);
			return invoicecounterx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Invoicecounter add(Invoicecounter invoicecounter) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(invoicecounter);
			em.getTransaction().commit();
			invoicecounter = refresh(invoicecounter);
		} finally {
			em.close();
			return invoicecounter;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Invoicecounter invoicecounter) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Invoicecounter invoicecounterx = em.find(Invoicecounter.class, invoicecounter.getId());
			invoicecounterx.setKey(invoicecounter.getKey());
			invoicecounterx.setId(invoicecounter.getId());
			invoicecounterx.setDtinv(invoicecounter.getDtinv());
			invoicecounterx.setInvnumb(invoicecounter.getInvnumb());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Invoicecounter refresh(Invoicecounter invoicecounter) {

		Invoicecounter invoicecounterx = invoicecounter;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			invoicecounterx = em.find(Invoicecounter.class, invoicecounter.getId());
			em.flush();
			em.refresh(invoicecounterx);
		} finally {
			em.close();
			return invoicecounterx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Invoicecounter invoicecounter) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Invoicecounter invoicecounterx = em.find(Invoicecounter.class, invoicecounter.getId());
			em.remove(invoicecounterx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
